package com.example.mallsystem1104.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mallsystem1104.annotation.Authority;
import com.example.mallsystem1104.common.Result;
import com.example.mallsystem1104.constants.Constants;
import com.example.mallsystem1104.entity.AuthorityType;
import com.example.mallsystem1104.entity.Order;
import com.example.mallsystem1104.service.OrderService;
import com.sun.xml.fastinfoset.stax.events.Util;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zy
 * @Date 2024/12/12 16:16
 * @packageName:com.example.mallsystem1104.controller
 * @ClassName:OrderController
 */
@RestController
@RequestMapping("/api/order")
@CrossOrigin
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 保存地址信息
     * @param order
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Order order){
        String orderNO=orderService.saveOrder(order);
        return Result.success(orderNO);
    }

    @GetMapping("/paid/{orderNo}")
    public Result payOrder(@PathVariable String orderNo){
        orderService.payOrder(orderNo);
        return Result.success();
    }

    @GetMapping("/userid/{userid}")
    public Result selectByUserId(@PathVariable int userid){
        return Result.success(orderService.selectByUserId(userid));
    }

    @GetMapping("/page")
    public Result findPage(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam String orderNo,
            @RequestParam String state
    ){
        IPage<Order> orderIPage=new Page<>(pageNum,pageSize);
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.ne("state","待付款");
        if(!Util.isEmptyString(orderNo)){
            queryWrapper.like("order_no",orderNo);
        }
        if(!Util.isEmptyString(state)){
            queryWrapper.eq("state",state);
        }
        queryWrapper.orderByDesc("create_time");
        return Result.success(orderService.page(orderIPage,queryWrapper));
    }

    @GetMapping("/orderNo/{orderNo}")
    public Result selectByOrderNo(@PathVariable String orderNo){
        return Result.success(orderService.selectByOrderNo(orderNo));
    }

    @GetMapping
    public Result findAll(){
        List<Order> list=orderService.list();
        return Result.success(list);
    }

    //发货
    @Authority(AuthorityType.requireAuthority)
    @GetMapping("/delivery/{orderNo}")
    public Result delivery(@PathVariable String orderNo){
        orderService.delivery(orderNo);
        return Result.success();
    }

    //确认收货
    @GetMapping("/received/{orderNo}")
    public Result receiverOrder(@PathVariable String orderNo){
        if (orderService.receiveOrder(orderNo)){
            return Result.success();
        }else {
            return Result.error(Constants.CODE_500,"确认收货失败");
        }
    }


    @PutMapping
    public Result update(@RequestBody Order order){
        orderService.updateById(order);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        orderService.removeById(id);
        return Result.success();

    }

}
